﻿$(function(){
	var $slider = $("#goodpic") , //取得圖片輪播區塊
	$ul = $("#goodpic > ul"),
	$li = $("ul li", $slider),//取得區塊內所有li
	fadeSpeed = 400,
	$a = $("li a");
	
	var $smallpic = $("#smallpic img");
	
	$("#prev").click(function() {
		$(".add").removeClass("selected");
		$(".add").remove();
		var $selected = $li.filter(".selected");
		var no = $selected.index();
		no = this.id == "prev" ? no - 1 : no + 1;
		if(no == $li.length) {
			no = 0;
		} else if(no < 0) {
			no = $li.length - 1;
		}	
		$li.eq(no).fadeTo(fadeSpeed + 100 ,1).addClass('selected').siblings().fadeTo(fadeSpeed, 0).removeClass('selected');
		
		return false;
	});
	
	$("#next").click(function() {
		$(".add").removeClass("selected");
		$(".add").remove();
		var $selected = $li.filter(".selected");
		var no = $selected.index();
		no = this.id == "prev" ? no - 1 : no + 1;
		if(no == $li.length) {
			no = 0;
		} else if(no < 0) {
			no = $li.length - 1;
		}
		$li.eq(no).fadeTo(fadeSpeed + 100 ,1).addClass('selected').siblings().fadeTo(fadeSpeed, 0).removeClass('selected');
		
		return false;
	});
	
	$smallpic.mouseover(function(){
		$(".add").removeClass("selected");
		$(".add").remove();
		var $this = $(this);
		var $selected = $li.filter(".selected");
		$selected.removeClass("selected");
		$ul.append('<li class="add"><a href="' + $this.attr('src') + '" class="lightbox"><img src="' + $this.attr('src')+'"/></a></li>');
		$(".add").fadeTo(fadeSpeed + 100 ,1).addClass('selected').siblings().fadeTo(fadeSpeed, 0).removeClass('selected');
	});
 
	$ul.on('click', 'li', function(){
		$('.lightbox').lightBox();
		return false;
	});

});